<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.loginName" placeholder="用户账号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.realName" placeholder="用户名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.email" placeholder="用户邮箱" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <div style="float:right;">
        <el-button class="filter-item" @click="$router.back(-1)">返回</el-button>
        <el-button class="filter-item" type="" style="margin-left: 10px;" icon="el-icon-edit" @click="addUser">添加用户</el-button>
        <el-button class="filter-item" type="" style="margin-left: 10px;" icon="el-icon-circle-close" @click="delRoleUser('ALL')">取消指派</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </div>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      stripe
      fit
      highlight-current-row
      @selection-change="handleRoleUserChange"
      style="width: 100%;border-top: 1px solid #dfe6ec;border-left: 1px solid #dfe6ec;border-right: 1px solid #dfe6ec;">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="用户账号" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.loginName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职位" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
                  <span v-for="item in positions">
                    <span v-if="item.itemKey == scope.row.position">{{ item.itemValue }}</span>
                  </span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户邮箱" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.createTime">{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="搜索" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tooltip class="item action-icon" effect="dark" content="取消指派" placement="top">
            <a><svg-icon icon-class="ljm-cancel-ref" class="action_icon icon-size" @click="delRoleUser(scope.row)"/></a>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <!--用户信息-->
    <el-dialog title="选择用户" :visible.sync="dialogTableVisible">
      <div class="app-container">
        <div class="filter-container">
          <el-input v-model="userListQuery.loginName" placeholder="用户账号" style="width: 200px;" class="filter-item" @keyup.enter.native="userHandleFilter" />
          <el-input v-model="userListQuery.realName" placeholder="用户名称" style="width: 200px;" class="filter-item" @keyup.enter.native="userHandleFilter" />
          <el-input v-model="userListQuery.email" placeholder="邮箱" style="width: 200px;" class="filter-item" @keyup.enter.native="userHandleFilter" />
          <div style="float:right;">
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="userHandleFilter">搜索</el-button>
          </div>
        </div>

        <el-table
          :key="userTableKey"
          v-loading="userListLoading"
          :data="userList"
          stripe
          fit
          highlight-current-row
          @selection-change="handleSelectionChange"
          style="margin-left: 1px;width: 100%;border-top: 1px solid #dfe6ec;border-left: 1px solid #dfe6ec;border-right: 1px solid #dfe6ec;">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column label="名称" align="left">
            <template slot-scope="scope">
              <span>{{ scope.row.realName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="职位" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
                  <span v-for="item in positions">
                    <span v-if="item.itemKey == scope.row.position">{{ item.itemValue }}</span>
                  </span>
            </template>
          </el-table-column>
          <el-table-column label="账号" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.loginName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="身份证" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.identityNo }}</span>
            </template>
          </el-table-column>
          <el-table-column label="联系方式" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.mobile }}</span>
            </template>
          </el-table-column>
          <el-table-column label="邮箱" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.email }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status | statusTypeFilter">
                {{ scope.row.status | statusFilter }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="userTotal>0" :total="userTotal" :page.sync="userListQuery.pageNo" :limit.sync="userListQuery.pageSize" @pagination="getList" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { delRoleUser, getRefUserPage, setUserRef } from '@/api/sys/auth/role/index'
import { getList } from '@/api/org/user/index'
import { getItemByDictKey } from '@/api/sys/dict/item/index'

export default {
    name: 'roleUserRef',
    components: { Pagination },
    filters: {
      statusTypeFilter(status) {
        const statusTypeMap = {
          1: 'primary',
          0: 'danger'
        }
        return statusTypeMap[status]
      },
      statusFilter(status) {
        const statusMap = {
          1: '启用',
          0: '禁用'
        }
        return statusMap[status]
      }
    },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        realName: '',
        roleCode: this.$route.query.roleCode,
        loginName: '',
        email: ''
      },
      formData: {
        userIds: [],
        roleCode: this.$route.query.roleCode
      },
      roleUserIds: [],
      rules: {
        userIds: {

        }
      },
      dialogTableVisible: false,
      userTableKey: 0,
      userList: [],
      userTotal: 0,
      userListLoading: true,
      userListQuery: {
        pageNo: 1,
        pageSize: 10,
        realName: '',
        loginName: '',
        email: ''
      },
      positions: []
    }
  },
  created() {
    if (this.$route.query.roleCode) {
      this.getList()
      this.getPosition('position')
    } else {
      this.$router.push({ name: 'role_mgr_list' })
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      getRefUserPage(this.listQuery).then(response => {
        if(response.data.records){
          this.list = response.data.records
        }
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    submitForm() {
      if (this.formData.userIds) {
        this.loading = true
        setUserRef(this.formData).then(() => {
          this.handleFilter()
          this.loading = false
          this.dialogTableVisible = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.$message({
          showClose: true,
          message: '请选择用户',
          type: 'danger'
        })
        return false
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getUserList() {
      this.userListLoading = true
      getList(this.userListQuery).then(response => {
        this.userList = response.data.records
        this.userTotal = response.data.total
        setTimeout(() => {
          this.userListLoading = false
        }, 1.5 * 1000)
      })
      this.userListLoading = false
    },
    userHandleFilter() {
      this.userListQuery.pageNo = 1
      this.getUserList()
    },
    addUser(){
      this.dialogTableVisible = true
      this.getUserList()
    },
    getPosition(dictKey) {
      getItemByDictKey(dictKey).then(res => {
        this.positions = res.data
      })
    },
    handleSelectionChange(val) {
      val.forEach(item =>{
        this.formData.userIds.push(item.id)
      });
    },
    delRoleUser(data){
      this.$confirm('此操作将取消该用户的关联, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        if(data !== 'ALL'){
          this.roleUserIds=[data.id]
        }
        delRoleUser(this.roleUserIds).then(() => {
          this.getList()
          this.listLoading = false
        }).catch(
          this.listLoading = false
        )
        this.$message({
          type: 'success',
          message: '取消关联成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消关联失败'
        })
      })
    },
    handleRoleUserChange(val){
      val.forEach(item =>{
        this.roleUserIds.push(item.id)
      });
    }
  }
}
</script>
<style scoped>
  .action-icon{
    padding: 0 5px;
  }
  .icon-size{
    width: 17px;
    height: 17px;
  }
</style>
